package com.ts.api.http.org.biz;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.algorithm.encrypt.AlgorithmPadding;
import com.gitee.apanlh.util.algorithm.encrypt.symmetric.AES;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.ts.api.http.org.entity.apply.OrgApplyLongYanQiNuoJinHua;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.core.utils.ApiFieldConverter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMd5ConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5Apply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5DataExchange;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 安心-聚晟鑫-上海 单公拒量
 *
 * @author WJB
 */
@ApiPlan(productId = "413", type = ApiPlanConvertType.ORG_PHONE_MD5)
public class ApiOrgPhoneMd5JuShengXin implements ApiOrgPhoneMd5ConvertHandler {
    private static final String KEY = "GowJu6UkV96H0zj6";
    private static final String IV = "CSosjWbju3cybZaA";
    private static final Integer FILED_ID = 438;

    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) throws Exception {
        Map<Object, Object> newMap = new LinkedHashMap<>();
        newMap.put("mobile", apiDataExchangeParameter.getPhoneMd5());
        return newMap;
    }

    @Override
    public RespOrgPhoneMd5DataExchange respDataExchange(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        boolean code = Eq.object(jsonObject.getInteger("code"), 0);
        if (!code) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        return RespOrgPhoneMd5DataExchange.ofSuc();
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) throws Exception {
        OrgApplyLongYanQiNuoJinHua orgApplyLongYanQiNuoJinHua = new OrgApplyLongYanQiNuoJinHua();
        //  拓展
        ApiFieldConverter apiFieldConverter = ApiFieldConverter.create(apiApplyParameter);
        //  file_id
        orgApplyLongYanQiNuoJinHua.setFile_id(FILED_ID);
        //  姓名
        AES aes = AES.createCbc(KEY.getBytes(), IV.getBytes(), AlgorithmPadding.ZERO);
        orgApplyLongYanQiNuoJinHua.setName(aes.encryptToBase64Str(apiApplyParameter.getUserName()));
        //  手机号码
        orgApplyLongYanQiNuoJinHua.setMobile(aes.encryptToBase64Str(apiApplyParameter.getPhone()));
        //  年龄(若没有传0)
        orgApplyLongYanQiNuoJinHua.setAge(apiFieldConverter.getAgeInt());
        //  性别(1:为男,2:为女,无传0)
        Integer newSex = ChooseEq.create()
                .when(apiApplyParameter.getSex(), "0", 1)
                .when(apiApplyParameter.getSex(), "1", 2)
                .end(0);
        orgApplyLongYanQiNuoJinHua.setSex(newSex);
        //  城市(如天津市)
        orgApplyLongYanQiNuoJinHua.setCity(apiApplyParameter.getCity());
        //  是否有房(是传1,否传0)
        orgApplyLongYanQiNuoJinHua.setIs_house(apiFieldConverter.hasRealEstateInfoToNumber());
        //  是否有车(是传1,否传0)
        orgApplyLongYanQiNuoJinHua.setIs_car(apiFieldConverter.hasCarInfoToNumber());
        //  是否有公司营业执照(是传1,否传0)
        orgApplyLongYanQiNuoJinHua.setIs_company(apiFieldConverter.hasBizLicenseYearToNumber());
        //  是否有信用卡 (是传1,否传0)
        orgApplyLongYanQiNuoJinHua.setIs_credit(apiFieldConverter.hasCreditLimitToNumber());
        //  是否有保单(是传1,否传0)
        orgApplyLongYanQiNuoJinHua.setIs_insurance(apiFieldConverter.hasInsurancePolicyToNumber());
        //  是否有社保(是传1,否传0)
        orgApplyLongYanQiNuoJinHua.setIs_social(apiFieldConverter.hasSocialSecurityToNumber());
        //  是否有公积金(是传1,否传0)
        orgApplyLongYanQiNuoJinHua.setIs_fund(apiFieldConverter.hasProvidentFundToNumber());
        //  是否有打卡工资(是传1,否传0)
        Integer newWork = ChooseEq.create()
                .when(apiApplyParameter.getSalaryPayMethod(), "1", 1)
                .when(apiApplyParameter.getSalaryPayMethod(), "2", 0)
                .when(apiApplyParameter.getSalaryPayMethod(), "3", 0)
                .end(0);
        orgApplyLongYanQiNuoJinHua.setIs_work(newWork);
        //  是否有营业税(是传1,否传0)
        orgApplyLongYanQiNuoJinHua.setIs_tax(0);
        //  是否有微粒贷(是传1,否传0)
        orgApplyLongYanQiNuoJinHua.setWebank(0);
        //  申请额度（单位:元）
        orgApplyLongYanQiNuoJinHua.setMoney_demand(apiFieldConverter.getLoanAmountInt());
        //  备注
        orgApplyLongYanQiNuoJinHua.setContent(apiApplyParameter.getFormRemark());
        return JsonUtils.toJson(orgApplyLongYanQiNuoJinHua);
    }

    @Override
    public RespOrgPhoneMd5Apply respApply(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        boolean code = Eq.object(jsonObject.getInteger("code"), 0);
        if (!code) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        return RespOrgPhoneMd5Apply.ofSuc();
    }
}
